<?php
/* 
* @Author: GunFox
* @Date:   2014-05-13 23:23:11
* @Last Modified by:   GunFox
* @Last Modified time: 2014-05-14 01:11:20
*/
class GoodsTypeModel extends Model{
	public $table= 'gfgoodstype';
	//添加商品类型
	public function addGoodsType($Data){
		if(empty($Data)){
			$this->error='数据不能为空';
			return false;
		}
		$this->validate=array(
			array('GF_gt_name','nonull','类型名不能为空',2,1)
		);
		if($this->create($Data)){
			$db = M('gfgoodstype');
			if($db->where(array('GF_gt_name'=>$Data['GF_gt_name']))->find()){
				$this->error='类型名已经存在';
				return false;
			}
			$state = $this->add();
			if($state){
				if($this->updateCache()){
					return $state;
				}
			}
			return false;
		}else{
			return false;
		}
	}
	public function delGoodsType($Data){
		if(empty($Data)){
			return ture;
		}else{
			$state=$this->where(array('GF_gt_id'=>$Data))->del();
			if($state){
				if($this->updateCache()){
					return $state;
				}
			}return false;
		}
		
	}
	public function editGoodsType($Data){
		if(empty($Data)){
			$this->error='数据不能为空';
			return false;
		}
		$this->validate=array(
			array('GF_gt_name','nonull','类型名不能为空',2,1)
		);
		if($this->create($Data)){
			$db = M('gfgoodstype');
			if($db->where(array('GF_gt_name'=>$Data['GF_gt_name']))->find()){
				$this->error='类型名已经存在';
				return false;
			}
			$state = $this->where(array('GF_gt_id'=>$Data['GF_gt_id']))->save();
			if($state){
				if($this->updateCache()){
					return $state;
				}
			}
			return false;
		}else{
			return false;
		}
	}
	//更新缓存
	public function updateCache(){
		$GoodsTypeData = $this->all();
		$cacheData=array();
		foreach($GoodsTypeData as $GoodsType){
			$cacheData[$GoodsType['GF_gt_id']]=$GoodsType;
		}
		return cache("gfgoodstype",$cacheData);
	}
}